import React, { Component } from 'react'
import store from '../../redux/store';
// 引入store ，用于获取redux中的状态

export default class Count extends Component {
    // // 检测redux中状态的变化，只要变化就调用render
    // componentDidMount(){
    //     store.subscribe(()=>{
    //         this.setState({})
    //     })
    // }
    
    // 加法
    increment=()=>{
        const {value}=this.SelectNum;
        store.dispatch({type:"increment",data:value*1})
        
    }
    // 减法
    decrement=()=>{
        const {value}=this.SelectNum;
        store.dispatch({type:"decrement",data:value*1})
    }
    // 奇数再加
    incrementIfOdd=()=>{
        const {value}=this.SelectNum;
        const count=store.getState()
        if(count%2!==0){
            store.dispatch({type:"increment",data:value*1})
        }
    }
    // 异步加
    incrementAsync=()=>{
        const {value}=this.SelectNum;
        setTimeout(()=>{
            store.dispatch({type:"increment",data:value*1})
        },1000)
    }
    render() {
        return (
            <div>
                <h1>当前求和为：{store.getState()}</h1>
                <select ref={c=>this.SelectNum=c} name="number" id="">
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                </select>
                <button onClick={this.increment}>+</button>
                <button onClick={this.decrement}>-</button>
                <button onClick={this.incrementIfOdd}>当前求和为奇数加</button>
                <button onClick={this.incrementAsync}>异步加</button>
            </div>
        )
    }
}
